# Todo list:
# the content can't copy to clipboard with format

# Create a user agent object
use LWP::UserAgent;
use HTML::Manipulator;


use Win32::Clipboard;
$CLIP = Win32::Clipboard();


$| = 1;

use Encode;

$ua = LWP::UserAgent->new;
$ua->agent("MyApp/0.1 ");

print "Please input the url you want to save:\n";
chomp($url = <STDIN>);

my $outfile1 = ">content.txt";
open(out_dev, $outfile1);



# Create a request
my $req = HTTP::Request->new(POST => $url);
$req->content_type('application/x-www-form-urlencoded');
$req->content('query=libwww-perl&mode=dist');

# Pass request to the user agent and get a response back
my $res = $ua->request($req);

# Check the outcome of the response
if ($res->is_success) {
    if ( $url =~ m/sohu\.com/ ) {
        #the specific function for sohu blog
        &extract_from_sohu_blog;
    } elsif ( $url =~ m/sina\.com\.cn/ ) {
        #the specific function for sina blog
        &extract_from_sina_blog;
    } else {
        print "Can't select the right function.\n";
    }
}
else {
    print $res->status_line, "\n";
}



## sub function for delete html code
sub delHtml{
    my $str = shift;
    $str =~ s/<(style|script|iframe|object|embed)(?=\s|>).+?<\/\1>//igs;
    $str =~ s/on(click|mouseover|mousedown|load)=".+?"//igs;
    $str =~ s/on(click|mouseover|mousedown|load)=.+?\s+?//igs;
    $str =~ s/<p ALIGN=\"center\">.*<\/FONT><\/A><\/P>//gs;
    $str =~ s/&nbsp;//igs;
    $str =~ s/<\/SPAN><\/P>/\r\n\r\n/gs;
    $str =~ s/<\/FONT><\/P>/\r\n\r\n/gs;
    $str =~ s/<[^>]+?>//gs;


    return $str;
}

## sub function to drag content from sina blog
## the url is something like: http://blog.sina.com.cn
sub extract_from_sina_blog {
    my $title = HTML::Manipulator::extract_title($res->content);
    #my $title = HTML::Manipulator::extract_content($res->content, 'articleTitle');
    my $content = HTML::Manipulator::extract_content($res->content, 'articleBody');
    #print $res->content;
    #print out_dev ($res->content) or die "can't write into new file\n";
    print out_dev $title."\n\n\n";

    #copy tille to clipboard
    $CLIP->Set(encode("gbk", decode("UTF-8", $title)));

    print "The article title is copied into clipboard, please enter a key if you finish using it!";
    <STDIN>;
    
    
    print out_dev "Original url: $url\n";
    $content = &delHtml($content);
    
    print out_dev $content;

    
    #copy content to clipboard
    $CLIP->Set(encode("gbk", decode("UTF-8", $title."\r\n\r\n"."Original url: $url\r\n\r\n".$content)));
    print "The full content is copied into clipboard. Got it!?\n";
    
    print "done.\n";
}

## sub function to extract content from sohu blog
## the url is something like: http://shihb.blog.sohu.com
sub extract_from_sohu_blog {
    my $title = HTML::Manipulator::extract_title($res->content);
    #my $title = HTML::Manipulator::extract_content($res->content, 'articleTitle');
    my $content = HTML::Manipulator::extract_content($res->content, 'main-content');
    #print $res->content;
    #print out_dev ($res->content) or die "can't write into new file\n";
    print out_dev $title."\n\n\n";

    #copy tille to clipboard
    $CLIP->Set( $title );

    print "The article title is copied into clipboard, please enter a key if you finish using it!";
    <STDIN>;
    
    
    print out_dev "Original url: $url\n";
    $content = &replace_sohu_html($content);
    
    print out_dev $content;

    
    #copy content to clipboard
    $CLIP->Set( $title."\r\n\r\n"."Original url: $url\r\n\r\n".$content);
    print "The full content is copied into clipboard. Got it!?\n";
    
    print "done.\n";
}

## sub function for replace sohu html code
sub replace_sohu_html{
    my $str = shift;
    $str =~ s/&mdash;/--/igs;
    $str =~ s/&ldquo;/"/igs;
    $str =~ s/&rdquo;/"/igs;
    $str =~ s/<br \/>/\r\n\r\n/igs;
    $str =~ s/&hellip;/.../igs;

    #delete the bottom link part
    $str =~ s/<p><a href=.*<\/a><\/p>//igs;

    #delete the bottom previous and next link
    $str =~ s/<div class="previous_txt">.*<\/div>//igs;
    $str =~ s/<div class="next_txt">.*<\/div>//igs;
    
    $str =~ s/<(style|script|iframe|object|embed)(?=\s|>).+?<\/\1>//igs;
    $str =~ s/on(click|mouseover|mousedown|load)=".+?"//igs;
    $str =~ s/on(click|mouseover|mousedown|load)=.+?\s+?//igs;
    $str =~ s/&nbsp;/ /igs;
    $str =~ s/<\/font><\/p>/\r\n\r\n/gs;
    $str =~ s/<[^>]+?>//gs;
    return $str;
}
